#include <stdio.h>
char get_first(void);
char get_choice(void);
float get_float(void);
float add(float a, float b);
float subtract(float a, float b);
float multiply(float a, float b);
float divide(float a, float * b);
int main(void)
{
	char choice;
    float f_op;
    float s_op;

	while ((choice = get_choice()) != 'q')
	{
		switch(choice)
		{
			case 'a' :
			    printf("Add: \n");
			    printf("Enter first number: ");
                f_op = get_float();
                printf("Enter second number: ");
                s_op = get_float();
			    printf("%.2f + %.2f = %.2f \n", f_op, s_op,add(f_op, s_op));
				break;
			case 's' :
			    printf("Subtract: \n");
                printf("Enter first number: ");
                f_op = get_float();
                printf("Enter second number: ");
                s_op = get_float();
			    printf("%.2f - %.2f = %.2f \n", f_op, s_op, subtract(f_op, s_op));
				break;
			case 'm' :
			    printf("Multiply: \n");
			    printf("Enter first number: ");
                f_op = get_float();
                printf("Enter second number: ");
                s_op = get_float();
                printf("%.2f * %.2f = %.2f \n", f_op, s_op, multiply(f_op, s_op));
				break;
			case 'd' :
			    printf("Divide: \n");
			    printf("Enter first number: ");
                f_op = get_float();
                printf("Enter second number: ");
                s_op = get_float();
                printf("%.2f / %.2f = %.2f \n", f_op, s_op, divide(f_op, &s_op));
				break;
			default : printf("Program Error!");
				break;
		}
		getchar();
	}
	printf("Bye. \n");

	return 0;
}
char get_first(void)
{
	char ch;

	ch = getchar();
	while (getchar() != '\n')
		continue;

	return ch;
}
char get_choice(void)
{
	char ch;

    printf("Enter the operation of your choice: \n");
	printf("a. add		    s. subtract \n");
	printf("m. multiply 	    d. divide \n");
	printf("q. quit\n");
	ch = get_first();
	while (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
	{
		printf("Please respond with a,s,m,d, or q :");
		ch = get_first();
	}

	return ch;
}
float get_float(void)
{
	float input;
	char ch;

	while (scanf("%f", &input) != 1)
	{
		while ((ch = getchar()) != '\n')
			putchar(ch);
        printf(" is not a integer. \n");
        printf("Please enter a number such as 2.5, -1.78E8 or 3:");
	}

	return input;
}

float add(float a, float b)
{
	return (a + b);
}

float subtract(float a, float b)
{
	return (a - b);
}

float multiply(float a, float b)
{
	return (a * b);
}

float divide(float a, float * b)
{
	if (*b == 0)
	{
		printf("Enter a number other than 0: ");
		*b = get_float();
	}
	return a / *b;
}